﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace QSearcher.QCrawler
{
    class Requester
    {
        public void Run(int index)
        {
            FrmCrawler.T.SendMsg("开始请求链接数据......");
            var resUrl = BizLayer.Self.GetRequestUrl();
            if (resUrl.Code != 0)
            {
                FrmCrawler.T.SendMsg("未请求到链接数据.");
                return;
            }
            FrmCrawler.T.SendMsg("请求到有效的链接数据.");

            string msg = string.Empty;
            string responseBody = string.Empty;
            try
            {
                FrmCrawler.T.SendMsg("开始获取网页.......");
                HttpWebRequest request = HttpWebRequest.Create(resUrl.Url) as HttpWebRequest;
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;

                if (response.ContentEncoding.ToLower().Contains("gzip"))
                {
                    using (GZipStream stream = new GZipStream(response.GetResponseStream(), CompressionMode.Decompress))
                    {
                        using (StreamReader sr = new StreamReader(stream))
                        {
                            responseBody = sr.ReadToEnd();
                        }
                    }
                }
                else if (response.ContentEncoding.ToLower().Contains("deflate"))
                {
                    using (DeflateStream stream = new DeflateStream(response.GetResponseStream(), CompressionMode.Decompress))
                    {
                        using (StreamReader sr = new StreamReader(stream))
                        {
                            responseBody = sr.ReadToEnd();
                        }
                    }
                }
                else
                {
                    using (Stream stream = response.GetResponseStream())
                    {
                        using (StreamReader sr = new StreamReader(stream, Encoding.GetEncoding(response.CharacterSet)))
                        {
                            responseBody = sr.ReadToEnd();
                        }
                    }
                }
                FrmCrawler.T.SendMsg("成功获取网页.");


                var file = "Requests" + Path.DirectorySeparatorChar + Crawler.UrlToPath(resUrl.Url);
                Crawler.CheckDirectory(file);
                File.WriteAllText(file, responseBody, Encoding.Default);
            }
            catch (Exception e)
            {
                msg = e.Message;
            }

            BizLayer.Self.SendRequestResult(new BizLayer.RequestResult()
            {
                ID=resUrl.ID,
                Msg=msg,
            });
            FrmCrawler.T.SendMsg("反馈成功.");
        }
    }
}
